// The URL where the extension is hosted
var host_url="http://widget.tringme.com/chrome";

var logincookie="tringmeexloginsession";
var closetimer	= 0;
var ddmenuitem	= 0;
var callprogress=0;
var phno=0;
var cookie="";
var autodial=0;
var timeout	= 500;

function FindPlacementPosition(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop+14;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop; 
		} 
	}
	return [curleft,curtop]; 
}

function DisplayMenu(parent,named,plink,phno){
	if(callprogress!=1)
	{
		if(document.getElementById("tr123box1")!=null)
			hide_box("tr123box1");
		
		var menu_element = document.getElementById(named);
		menu_element.style.display ="block";
		var placement = FindPlacementPosition(parent);
		menu_element.style.left = placement[0] + "px";
		menu_element.style.top = placement[1] + "px"; 
		var param="phno="+phno;
		mcancelclosetime();
		// close old layer
		if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
		// get new layer and show it
		ddmenuitem = document.getElementById(named);
		ddmenuitem.style.visibility = 'visible';
		
		cookie = GetCookie(logincookie);
		if (cookie == "") {
			param="op=get_cookie";
			CallServer(param);
		}
		
		
		ddmenuitem.innerHTML=GetMenuContents(phno,placement[0],placement[1],plink);		
	}
}

function AddMenuItem(text, img, action) {	
	menu_item = "<li class='tr123menu-items' ><img class='tr123menu-img'  src=\""+host_url+img+"\" height=\"14\" width=\"14\"/><span class='tr123menu-txt' onclick="+action+">"+text+"</span></li>";
	
	return menu_item;
}

function GetMenuContents(phno,left,top,plink){
	phno=ParsePhoneNumber(phno);

	var menu_items =  "<div id='tr123number'>+"+phno+"</div>";	
	
	menu_items +="<ul>";	
	
	menu_items += "<li style='list-style:none;' class='tr123menu-items' ><img class='tr123menu-img'  src=\""+host_url+"/images/menu_phone.png\" height=\"14\" width=\"14\"/><span class='tr123menu-txt' onclick=\"LoadBox('phone',"+left+","+top+",'tr123box1','"+phno+"')\"; >Call Now</span></li>";

	menu_items += "<li style='list-style:none;' class='tr123menu-items' ><img  src=\""+host_url+"/images/menu_sms.png\" height=\"14\" width=\"14\" class='tr123menu-img' /><span class='tr123menu-txt' onclick=\"LoadBox('sms',"+left+","+top+",'tr123box1','"+phno+"')\"; >Send SMS</span></li>";

	menu_items += "<li style='list-style:none;' class='tr123menu-items'><img  src=\""+host_url+"/images/nomic.png\" height=\"14\" width=\"14\" class='tr123menu-img' /><span class='tr123menu-txt' onclick=\"LoadBox('c2c',"+left+","+top+",'tr123box1','"+phno+"')\"; >No Mic?</span></li>";

	menu_items += "<li style='list-style:none;' class='tr123menu-items' ><img  src=\""+host_url+"/images/menu_buy.png\" height=\"14\" width=\"14\" class='tr123menu-img' /><span class='tr123menu-txt' onclick=\"BuyCredits()\"; >Buy Credits</span></li>";
	
	// Original link 
	if(plink!="") {
	  menu_items += "<li style='list-style:none;' class='tr123menu-items' ><img  src=\""+host_url+"/images/menu_link.png\" height=\"14\" width=\"14\" class='tr123menu-img' /><span class='tr123menu-txt' onclick=\"GoToURL('"+plink+"')\"; >OriginalLink</span></li>";

	}	
	
	// Logout
	if(cookie!="") {
	menu_items += "<li style='list-style:none;' class='tr123menu-items' ><img  src=\""+host_url+"/images/menu_logout.png\" height=\"14\" width=\"14\" class='tr123menu-img' /><span class='tr123menu-txt' onclick=\"DoLogout()\"; >Logout</span></li>";
	}	
	
	menu_items += "</ul>";		
	
	// Logo
	menu_items += "<div id='tr123tringme-logo' width='100%' style='background:#000' ><a href='#' onclick='GoToURL(\"http://tringme.com\");' ><img src=\""+host_url+"/images/tringmelogo.png\" height=\"21\" width=\"80\" /></a></div>";
	
	return menu_items;	
}

function GoToURL(plink) {
	var w = window.open();
    w.opener = null;
    w.document.location = plink;
}

function BuyCredits() {
	GoToURL("http://login.tringme.com/buy.php");
}

function TringMeSignUp(){
	GoToURL("http://login.tringme.com/signup.php");
}

function ParsePhoneNumber(phonenumber){
	var pregex=/[@\-\s\(\)\.]/g;					 
	var p=phonenumber.replace(pregex,'');
	return p;
}

function GetCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
				
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function SetPhoneNumber(phonenumber){
	phno= phonenumber;
}

function GetPhoneNumber(){
	return phno;
}

function SetAutodial(adial){
	autodial=adial;
}

// Functions invoked by TringMe's Flash Widget
function GetCookieInFlash(){
	return cookie;
}
function GetParamsInFlash(){
	var phoneobj=phno+"*"+cookie+"*"+autodial;
	return phoneobj;	
}

function DoLogout()
{
  SetCookie(logincookie,"",-365);
  cookie="";
  param="op=logout";
  CallServer(param);
  mcancelclosetime();
  if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';	
}

function LoadBox(type,left,top,named,phno)
{
	//Close the menu layer
	mcancelclosetime();
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	
	//Show the box
	var box_element = document.getElementById(named);
	box_element.style.display ="block";
	
	var inner_div=document.getElementById("tringphone");
	
	box_element.style.left = left + "px";
	box_element.style.top = top + "px"; 
	
	if(cookie=="")
		cookie=GetCookie(logincookie);
	
	if(type=="login") {
		ShowLoginBox(inner_div,left,top,phno,"");
	}
	else if(type=="phone") {
		if(cookie!="") {			
			SetPhoneNumber(phno);
			autodial=CheckValidPhoneNumber(phno);
			SetAutodial(autodial);
			var content_div=document.getElementById("tr123content");
			content_div.style.background="";			
			SetBoxDimensions('phone');
			ShowFlashWidget();
		}
		else {
			var content_div=document.getElementById("tr123content");
			content_div.innerHTML="";
			var tr="<div id=\"tringphone\"></div>";
			content_div.innerHTML=tr;
			ShowLoginBox(inner_div,left,top,phno,type);
		}
	}
	else if(type=="c2c")
	{
		if(cookie!="") {
					  
			SetBoxDimensions('c2c');					  
			ShowC2CBox(phno);
		}
		else
			ShowLoginBox(inner_div,left,top,phno,type);
	}
	else if(type=="sms") {
		if(cookie!="") {
			ShowElement('close-btn',1);
			ShowElement('login-close',0);			
			SetBoxDimensions('sms');
			ShowSMSBox(phno);
		}
		else{
			ShowLoginBox(inner_div,left,top,phno,type);
		}
	}
	else
		box_element.innerHTML="error";
}

function ShowElement(id, bShow) {
	if(document.getElementById(id)!=null)
		document.getElementById(id).style.display=(bShow==1?"block":"none");
}

function ShowLoginBox(inner_div,left,top,phno,type)
{
		var inner_div=document.getElementById("tringphone");
		ShowElement('close-btn', 0);
		ShowElement('login-close', 1);
		
		SetBoxDimensions('login');
		inner_div.innerHTML=LoadLogin(left,top,phno);
		SetCookie("boxtype",type);
}

function callended()
{   
	callprogress=0;
}

function callestablished()
{
	callprogress=1;
}

function closeFlash()
{
	hide_box('tr123box1');
}

function SetBoxDimensions(type){	
	var box=document.getElementById("tr123box1");
	var content_div=document.getElementById('tr123content');
	if( (type=="c2c") || (type=="sms")) {
		box.style.border="0px";
		box.style.width="215px";
		box.style.height="155px";
		content_div.style.paddingTop="10px";
		content_div.style.width="215px";
		content_div.style.height="155px";
		box.style.backgroundColor="transparent";
		if (type == "c2c")
			content_div.style.background="url("+host_url+"/images/bk3.png) no-repeat";
		else
			content_div.style.background="url("+host_url+"/images/bk4.png) no-repeat";		
	}
	else if(type=="phone"){
		box.style.border="0px";
		content_div.style.paddingTop="0px";
		box.style.height="155px";		
		box.style.backgroundColor="transparent";
		content_div.background="transparent";
	}
	else if(type=="login"){
		box.style.width="172px";
		box.style.height="155px";	
		content_div.style.height="155px";
		box.style.backgroundColor="transparent";
		content_div.style.width="172px";
		content_div.style.background="url("+host_url+"/images/login.png) no-repeat";
	}
}

// Simple validation to check for valid phone numbers
function CheckValidPhoneNumber(phno){
	if(phno.length >= 10)
		return 1;
	else
		return 0;
}

function ShowSMSBox(phno)
{
	var sms="<div id='tringphone' style='padding:2px;padding-left:4px;visibility:visible;' >";
	sms += "<div style='padding-left:3px;color:#fff;visibility:visible;'>Enter your message:</div>";
	sms += "<textarea name='msg' cols='15' rows='2' id='msg' style='font-size:11px;visibility:visible;'></textarea>";
	sms += "<input type='hidden' value="+phno+" name='calldest' id='calldest' />";
	sms += "<div style='margin-top:0px;height:31px;padding-left:0px;font-size:10px;margin-top:1px;margin-bottom:2px;visibility:visible;'>";
	sms += "<div style='float:left;width:80px;visibility:visible;' id=\"tr123message\"></div>";
	sms += "<div style='float:right;margin-right:70px;width:27px;visibility:visible;height:30px;cursor:pointer;' onclick=FormSubmit('sms')>&nbsp;</div>";
	sms += "</div></div>";

	document.getElementById('tr123content').innerHTML="";
	document.getElementById('tr123content').innerHTML=sms;
	
}

function ChangeBox(left,top,phno)
{
	hide_box('tr123box1'); 
		mcancelclosetime();
  // close old layer
  	var btype=GetCookie("boxtype");			
	if(btype!="")
	{
		LoadBox(btype,left,top,"tr123box1",phno);
	}
	else
	{
		
		if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
		ddmenuitem.style.display='block';
		mcancelclosetime();
		  // get new layer and show it
		ddmenuitem = document.getElementById('tr123menu1');
		ddmenuitem.style.visibility = 'visible';
		ddmenuitem.innerHTML=GetMenuContents(phno,left,top);
		//DisplayMenu(parent,'tr123menu1',phno);
	}
	
		
}

function SetCookie(c_name,value,expiredays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

// close showed layer
function mclose(){
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime(){
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime(){
	if(closetimer){
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function DoNotSubmit(){
	return false;	
}

function ShowFlashWidget(){
	ShowElement('close-btn', 0);
	ShowElement('login-close', 0);
	var flashvars = { };
	var params = {  wmode:"transparent", allowScriptAccess:"always" };
	tringmechromeswfobject.embedSWF(host_url+"/swf/chromewidget.swf?","tringphone", "215", "138", "8.0.0",host_url+"/swf/expressInstall.swf", flashvars, params);
}

function FormSubmit(type){
	var param="";
	if(type=="c2c")
	{
		var callsrc=document.getElementById('callsrc').value;
		var calldest=document.getElementById('calldest').value;
		param="op=c2c&callsrc="+callsrc+"&calldest="+calldest;
	}
	else if(type=="sms")
	{
		var calldest=document.getElementById('calldest').value;
		var msg=document.getElementById('msg').value;
		param="op=sms&calldest="+calldest+"&msg="+msg;
	}
	else if(type=="login")
	{
	  var email=document.getElementById('email').value;
	  var password=document.getElementById('password').value;
	  var left=document.getElementById('left').value;
	  var top=document.getElementById('top').value;
	  var phno=document.getElementById('phno').value;
	  param="op=login&email="+email+"&password="+password+"&left="+left+"&top="+top+"&phno="+phno;
	}
	CallServer(param);
}

function LoadLogin(left,top,phno){
	var frm_content = "<a href=\'#\' style='margin-left:115px;visibility:visible;color:#fff;' onclick='TringMeSignUp()'>Signup</a>";
	frm_content += "<form action=\"#\" id=\"login-frm\" onsubmit=\"return DoNotSubmit()\" name=\"login-frm\" method=\"post\" >"; 
	frm_content += "<input type='hidden' name='left' id='left' value="+left+" />";
	frm_content += "<input type='hidden' name='top' id='top' value="+top+" />";
	frm_content += "<input type='hidden' name='phno' id='phno' value="+phno+" />";
    frm_content += "<div style='padding-top:3px;' style=\"visibility:visible\" class=\"tr123element-content\">";
    frm_content += "<div class=\"tr123form-elem-left\"' style=\"visibility:visible\" >Email ID:</div>";
    frm_content += "<div class=\"tr123form-elem-right\" style=\"visibility:visible\" ><input type=\"text\" style='font-size:11px;' id=\"email\" size=\"10\" name=\"email\"  /></div>";
	frm_content += "</div>";
    frm_content += "<div class=\"tr123element-content\" style=\"visibility:visible\" >";
    frm_content += "<div class=\"tr123form-elem-left\">Password:</div>";
    frm_content += "<div class=\"tr123form-elem-right\" style=\"visibility:visible\" ><input type=\"password\" style='font-size:11px;' size=\"10\" id=\"password\" name=\"password\"  /></div>";
    frm_content += "</div>";
	frm_content += "<div style='margin-top:0px;height:31px;padding-left:0px;font-size:10px;margin-left:1px;visibility:visible;margin-top:1px;margin-bottom:2px;'>";
	frm_content += "<div style='float:left;visibility:visible;width:80px;' id=\"tr123message\"></div>";
	frm_content += "<div style='float:right;margin-right:15px;height:30px;visibility:visible;cursor:pointer;width:33px;height:28px;' onclick=\"FormSubmit('login')\">&nbsp;</div>";
  	frm_content += "</form>";
	
	return frm_content;
}

function ShowC2CBox(phno){
	
	ShowElement('close-btn', 1);
	ShowElement('login-close', 0);
			
	var callsrc="";
	var c2c="<div id='tringphone' style='padding:2px;visibility:visible;' >";
 	c2c += "<div style='font-weight:normal;color:#fff;padding-left:3px;visibility:visible;' >We will call you back and connect. Your number:</div>";
	c2c += "<div style='font-weight:normal;color:#fff;padding-left:3px;visibility:visible;' ></div>";
	c2c += "<input type='hidden' id='calldest' name='calldest' value="+phno+" >";
	c2c += "<input type='text' id='callsrc' size='14' style='margin-left:3px;font-size:11px;' name='callsrc' value="+callsrc+">";
	c2c += "<div style='margin-top:0px;height:40px;padding-left:0px;font-size:10px;margin-left:0px;margin-top:1px;visibility:visible;margin-bottom:2px;'>";
	c2c += "<div style='float:left;width:80px;visibility:visible;' id=\"tr123message\"></div>";
	c2c += "<div style='position:absolute;width:31px;visibility:visible;height:25px;float:right;margin-left:115px;cursor:pointer;' onclick=FormSubmit('c2c')>&nbsp;</div>";
	c2c += "</div></div>";
	
	document.getElementById('tr123content').innerHTML="";
	document.getElementById('tr123content').innerHTML=c2c;
}

function hide_box(named){
	var menu_element = document.getElementById(named);
	menu_element.style.display = "none";
	callprogress=0;
}

function CallServer(param){
	var address=""+host_url+"/php/engine.php";
	var rand="&sid="+Math.random();
    var url = address+"?"+param+rand;
	headElement = document.getElementsByTagName("head").item(0);
    var scriptTag = document.createElement("script");
    scriptTag.setAttribute("id", "server-resp");
    scriptTag.setAttribute("type", "text/javascript");
    scriptTag.setAttribute("src", url);
	headElement.appendChild(scriptTag);
}

